<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="采购单号码" label-width="88px" prop="purchaseOrderNo">
        <el-input
          v-model="queryParams.purchaseOrderNo"
          placeholder="请输入采购单号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-200px"
        />
      </el-form-item>
      <el-form-item label="供应商名称" label-width="88px" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="采购类型" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型"
          clearable
          class="!w-160px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核" prop="isApproved">
        <el-checkbox
          v-model="queryParams.isApproved"
        />
      </el-form-item>
      <el-form-item label="打印" prop="isApproved">
        <el-checkbox
          v-model="queryParams.isApproved"
        />
      </el-form-item>
      <el-form-item label="重新计算幅宽是否按采购宽" label-width="180px" prop="isApproved">
        <el-checkbox
          v-model="queryParams.isApproved"
        />
      </el-form-item>
      <el-form-item label="报价单号" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入报价单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开单日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="请选择开单日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="原料类型" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择原料类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="立账" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="币别" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="汇率" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="报价单位" prop="purchaseType">
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="交货日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="请选择交货日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="提示" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入提示"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="基础金额" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入基础金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
      <div style="display: flex; justify-content: space-between;">
      <el-form-item>
        <el-button
          type="primary"
          @click="handleSaveData('save')"
        > 保存 </el-button>
        <el-button v-show="isDisabled"
          type="primary"
          plain
          @click="handleEdit"
        >
          <Icon icon="ep:edit" class="mr-5px" /> 修改
        </el-button>
        <el-button v-show="!isDisabled"
          type="primary"
          plain
          @click="handleSaveData('refresh')"
        >
          <Icon icon="ep:refresh" class="mr-5px" /> 撤销
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleSaveData('refresh')"
        >
          <Icon icon="ep:refresh" class="mr-5px" /> 刷新
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleCheck"
          v-hasPermi="['erp:pur-raw-material-purchase-order:create']"
        >
          <Icon icon="ep:check" class="mr-5px" /> 审核
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:pur-raw-material-purchase-order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        > 增加
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleCreateblank"
        >  空白行
        </el-button>
        <el-button
          type="primary"
          plain
          @click="handleDelete"
        > 删除
        </el-button>
      </el-form-item>
      </div>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs
      v-model="tabSelected"
    >
      <el-tab-pane label="明细" name="detail">
        <ReusableTable
          :isDisabled="isDisabled"
          :tableData="list"
          :columns="columnsdetail"
          :loading="loading"
          @button-click="handleTableButton"
          @selection-change="handleSelectionChange"
        />
      </el-tab-pane>
      <el-tab-pane label="汇总" name="overall">
        <ReusableTable
          :isDisabled="isDisabled"
          :tableData="list"
          :columns="columnsoverall"
          :loading="loading"
          @button-click="handleTableButton"
          @selection-change="handleSelectionChange"
        />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PurRawMaterialPurchaseOrderForm ref="formRef" @success="getList" />
  <!-- 子表的列表 -->
  
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PurRawMaterialPurchaseOrderApi, PurRawMaterialPurchaseOrderVO } from '@/api/erp/purrawmaterialpurchaseorder'
import PurRawMaterialPurchaseOrderForm from './PurRawMaterialPurchaseOrderForm.vue'

/** ERP采购管理-原料采购单主 列表 */
defineOptions({ name: 'PurRawMaterialPurchaseOrder' })

const message = useMessage() // 消息弹窗
const router = useRouter() // 路由对象
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PurRawMaterialPurchaseOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  purchaseOrderNo: undefined,
  supplierName: undefined,
  purchaseType: undefined,
  isApproved: undefined,
  createTime: []
})

const isDisabled = ref(true) // 整个页面是否可编辑

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const multipleSelection = ref([]) // 选中行

const tabSelected = ref('detail')

const columnsdetail = reactive([
  { label: '', prop: '', fixed: true, type: 'selection' },
  { label: '序号', prop: 'id', fixed: true },
  { label: '生产单号', prop: 'purchaseOrderNo'},
  { label: '客户编号', prop: 'supplierId' },
  { label: '客户名称', prop: 'supplierName' },
  { label: '材质编号', prop: 'approveTime' },
  { label: '材质名称', prop: 'approveTime' },
  { label: '长', prop: 'approveTime' },
  { label: '宽', prop: 'purchaseType' },
  { label: '数量', prop: 'purchaseType' },
  { label: '净板需求数量', prop: 'purchaseType', width: 140 },
  { label: '已送货数', prop: 'totalWeight' },
  { label: '宽开', prop: 'totalArea' },
  { label: '长开', prop: 'totalVolume' },
  { label: '打印', prop: 'purchaseType' },
  { label: '单价', prop: 'isApproved' },
  { label: '报价', prop: 'approver' },
  { label: '面积(m2)', prop: 'discount', width: 120 },
  { label: '单重', prop: 'basicAmount' },
  { label: '单位', prop: 'discount' },
  { label: '类型', prop: 'discountedAmount' },
  { label: '压线', prop: 'discountAmount' },
  { label: '备注', prop: 'taxRate' },
  { label: '结单', prop: 'taxAmount', type: 'checkbox' },
  { label: '交货日期', prop: 'taxType', type: 'date' },
  { label: '体积(m3)', prop: 'taxType', width: 120 },
  { label: '净板宽', prop: 'taxType' },
  { label: '净板长', prop: 'taxType' },
  { label: '压线类型', prop: 'taxType' },
  { label: '金额', prop: 'taxType' },
  { label: '送货类型', prop: 'taxType' },
  { label: '总长(m)', prop: 'taxType' },
  { label: '克重', prop: 'taxType' },
  { label: '采购类型', prop: 'taxType' },
  { label: '订单规格', prop: 'taxType' },
  { label: '产品货号', prop: 'taxType' },
  { label: '产品品名', prop: 'taxType' },
  { label: '供应商材质', prop: 'taxType', width: 120 },
  { label: '规格', prop: 'taxType' },
  { label: '特价', prop: 'taxType', type: 'checkbox' },
  { label: '客户订单号', prop: 'taxType', width: 120 },
  { label: '月结', prop: 'taxType', type: 'checkbox' },
  { label: '楞型', prop: 'taxType' },
  { label: '耐破', prop: 'taxType' },
  { label: '边压', prop: 'taxType' },
  { label: '计价长', prop: 'taxType' },
  { label: '计价长单位', prop: 'taxType', width: 120 },
  { label: '计价宽', prop: 'taxType' },
  { label: '计价宽单位', prop: 'taxType', width: 120 },
  { label: '原料类型', prop: 'taxType' },
  { label: '厚度', prop: 'taxType' },
  { label: '品质要求', prop: 'taxType' },
  { label: '长单位', prop: 'taxType' },
  { label: '宽单位', prop: 'taxType' },
  { label: '报价单位', prop: 'taxType' },
  { label: '优惠', prop: 'taxType' },
  { label: '附加费', prop: 'taxType' },
  { label: '总重', prop: 'taxType' },
  { label: '基础单价', prop: 'taxType' },
  { label: '对照名称', prop: 'taxType' },
  { label: '总面积', prop: 'taxType' },
  { label: '生产采购长', prop: 'taxType', width: 120 },
  { label: '生产采购宽', prop: 'taxType', width: 120 },
  { label: '总体积', prop: 'taxType' },
  { label: '生产单数量', prop: 'taxType', width: 120 },
  { label: '款式名称', prop: 'taxType' },
  {
    label: '操作',
    prop: 'action',
   
    slot: 'action',
    fixed: 'right',
    width: 60,
    buttons: [
      // { text: '编辑', action: 'edit', type: 'primary', link: true },
      { text: '删除', action: 'delete', type: 'danger', link: true }
    ]
  }
])
const columnsoverall = reactive([
  { label: '', prop: '', fixed: true, type: 'selection' },
  { label: '序号', prop: 'number', fixed: true },
  { label: '材质编号', prop: 'purchaseOrderNo', width: '120px' },
  { label: '材质名称', prop: 'supplierId', width: '120px' },
  { label: '供应商材质', prop: 'supplierName', width: '120px' },
  { label: '长', prop: 'supplierName' },
  { label: '宽', prop: 'approveTime', type: 'date', width: '120' },
  { label: '厚', prop: 'approveTime', type: 'date', width: '120' },
  { label: '数量', prop: 'purchaseOrderNo', width: '120px' },
  { label: '客户名称', prop: 'purchaseOrderNo', width: '120px' },
  { label: '入库数', prop: 'purchaseOrderNo' },
  { label: '单价', prop: 'purchaseOrderNo' },
  { label: '金额', prop: 'purchaseOrderNo' },
  { label: '附加费', prop: 'purchaseOrderNo' },
  { label: '报价', prop: 'purchaseOrderNo' },
  { label: '采购类型', prop: 'purchaseOrderNo' },
  { label: '送货类型', prop: 'purchaseOrderNo' },
  { label: '压线', prop: 'purchaseOrderNo' },
  { label: '总面积', prop: 'purchaseOrderNo' },
  { label: '总长', prop: 'purchaseOrderNo' },
  { label: '创建人', prop: 'purchaseOrderNo' },
  { label: '创建时间', prop: 'purchaseOrderNo' },
  { label: '长单位', prop: 'purchaseOrderNo' },
  { label: '宽单位', prop: 'purchaseOrderNo' },
  { label: '交期', prop: 'purchaseOrderNo', width: 120 },
  { label: '对照名称', prop: 'purchaseOrderNo' },
  { label: '总重量', prop: 'purchaseOrderNo' },
  { label: '总体积', prop: 'purchaseOrderNo' },
  { label: '备注1', prop: 'purchaseType' },
  { label: '备注2', prop: 'purchaseType' },
  { label: '备注3', prop: 'purchaseType' },
  { label: '备注4', prop: 'purchaseType' },
  { label: '备注5', prop: 'purchaseType' },
  { label: '备注6', prop: 'purchaseType' },
  {
    label: '操作',
    prop: 'action',
   
    slot: 'action',
    fixed: 'right',
    width: 200,
    buttons: [
      // { text: '编辑', action: 'edit', type: 'primary', link: true },
      { text: '删除', action: 'delete', type: 'danger', link: true }
    ]
  }
])

/** 查询列表 */
const getList = async () => {
  isDisabled.value = true
  loading.value = true
  try {
    const data = await PurRawMaterialPurchaseOrderApi.getPurRawMaterialPurchaseOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 选中行操作 */
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const handleTableButton = ({ action, row }) => {
  switch (action) {
    case 'delete':
      handleDelete(row.id)
      break
    case 'refresh':
      getList()
      break
    default:
      console.warn(`未处理的操作: ${action}`)
  }
}

/** 审核 */
const handleCheck = async () => {
  // check
}

/** 增加空白行 */
const handleCreateblank = () => {
  list.value = [{id: ''}, ...list.value]
}

/** 编辑 */
const handleEdit = () => {
  isDisabled.value = !isDisabled.value
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PurRawMaterialPurchaseOrderApi.deletePurRawMaterialPurchaseOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 保存 刷新 */
const handleSaveData = async (type: string) => {
  // 是否需要保存
  if(type === 'save') {
    console.log('保存数据')
    // await 
  }
  await getList()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurRawMaterialPurchaseOrderApi.exportPurRawMaterialPurchaseOrder(queryParams)
    download.excel(data, 'ERP采购管理-原料采购单主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

const toDetail = (row) => {
  router.push("purrawmaterialpurchaseorderdetail")
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>